#!/bin/bash

# 本地构建项目
npm run deploy:test || { echo "构建失败"; exit 1; }

# 定义变量
REMOTE_USER=ec2-user
REMOTE_HOST=ec2-13-229-209-178.ap-southeast-1.compute.amazonaws.com
REMOTE_PATH=/home/ec2-user/wwwroot/tradebox-h5-test
LOCAL_FILE=h5.test.tar.gz
KEY_PATH=./aws/tradebox-server.pem  # 改成你自己的私钥路径

# 上传构建产物
scp -i $KEY_PATH ./$LOCAL_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH || { echo "上传失败"; exit 1; }

# 远程执行部署命令
ssh -i $KEY_PATH $REMOTE_USER@$REMOTE_HOST << EOF
  cd $REMOTE_PATH && pwd
  sudo tar -zxvf $LOCAL_FILE -C ./
  sudo cp -rf ./dist/* ./
  rm -f $LOCAL_FILE
EOF

echo "✅ 部署完成"
